﻿// 2025/10/21: 首个版本

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using ASEva;
using ASEva.UIAvalonia;
using ASEva.Utility;

namespace CommonSource
{
    partial class GuestSyncSelector : DialogPanel
    {
        public GuestSyncSelector()
        {
            InitializeComponent();
            SetResizableMode(700, 300, 700, 600);
            language = new LanguageSwitch(Resources, AgencyLocal.GetAppLanguage());
            Title = language["title"];

            var model = new GuestSyncSelectorViewModel(language);
            model.InitializeItems();
            DataContext = model;
        }

        public String IconResourceName
        {
            set => Icon = ResourceLoader.Load(value)?.ToAvaloniaWindowIcon();
        }

        private LanguageSwitch language;
    }

    partial class GuestSyncSelectorViewModel(LanguageSwitch language) : ObservableObject
    {
        public partial class OptionItem(String title, bool _isChecked, String id) : ObservableObject
        {
            public String Title => title;

            [ObservableProperty]
            private bool isChecked = _isChecked;

            partial void OnIsCheckedChanged(bool value)
            {
                AgencyAsync.SetChannelGuestSyncFlag(id, value);
            }
        }

        [ObservableProperty]
        private OptionItem[] busChannelItems = [];

        [ObservableProperty]
        private OptionItem[] videoChannelItems = [];

        [ObservableProperty]
        private OptionItem[] pluginChannelItems = [];

        public async void InitializeItems()
        {
            var syncFlags = new Dictionary<string, bool>();
            for (int i = 0; i < ChannelNumber.Bus; i++)
            {
                syncFlags["bus." + (i + 1)] = false;
            }
            for (int i = 0; i < ChannelNumber.Video; i++)
            {
                syncFlags["video." + i] = false;
            }

            var pluginTitleTable = await AgencyAsync.GetPluginGuestSyncTable();
            var pluginGuestSyncIDs = pluginTitleTable.Keys.ToList();
            pluginGuestSyncIDs.Sort();

            foreach (var key in pluginGuestSyncIDs)
            {
                syncFlags[key] = false;
            }

            var syncKeys = await AgencyAsync.GetAllChannelGuestSyncKeys();
            foreach (var key in syncKeys)
            {
                if (syncFlags.ContainsKey(key)) syncFlags[key] = true;
            }

            var busChannelItems = new OptionItem[ChannelNumber.Bus];
            for (int i = 0; i < ChannelNumber.Bus; i++)
            {
                var id = "bus." + (i + 1);
                busChannelItems[i] = new OptionItem(language.Format("bus-channel", i + 1), syncFlags[id], id);
            }
            BusChannelItems = busChannelItems;

            var itemsText = await Task.WhenAll(new Task<String>[ChannelNumber.Video].Populate(i => AgencyAsync.GetChannelAliasName("video@" + i)));
            var videoChannelItems = new OptionItem[ChannelNumber.Video];
            for (int i = 0; i < ChannelNumber.Video; i++)
            {
                var id = "video." + i;
                videoChannelItems[i] = new OptionItem(itemsText[i], syncFlags[id], id);
            }
            VideoChannelItems = videoChannelItems;

            var pluginChannelItems = new OptionItem[pluginGuestSyncIDs.Count];
            for (int i = 0; i < pluginGuestSyncIDs.Count; i++)
            {
                var id = pluginGuestSyncIDs[i];
                pluginChannelItems[i] = new OptionItem(pluginTitleTable[id], syncFlags[id], id);
            }
            PluginChannelItems = pluginChannelItems;
        }
    }
}